package com.mc.mcgoweb.utils.compare;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public class TestCompare {

    public static void main(String[] args) {
        List<Products> products = new ArrayList<Products>();
        products.add(new Products("费罗列", 100, 30));
        products.add(new Products("love", 1000, 100));
        products.add(new Products("德芙", 10000000, 10));
        products.add(new Products("自行车", 10000, 899));
        products.add(new Products("行李箱", 8000, 569));
        products.add(new Products("泳衣", 100, 180));
//        Collections.sort(products);
//        System.out.println(products);
        Collections.sort(products, new Comparator<Products>() {
            @Override
            public int compare(Products o1, Products o2) {
                //-1 不调整顺序  0 顺序不变 1 顺序交换
                return o1.getPrice() > o2.getPrice() ? -1 : o1.getPrice() == o2.getPrice() ? 0 : 1;
            }
        });
        System.out.println(products);
        //定义比较器按照销量排序
        Comparator<Products> byName = new Comparator<Products>() {
            @Override
            public int compare(Products o1, Products o2) {
                return o1.getNum() < o2.getNum() ? -1 : (o1.getNum() == o2.getNum() ? 0 : 1);
            }
        };
        Collections.sort(products, byName);
        System.out.println(products);
        //定义比较器按照销量升序排列如果销量一样的话按照价格降序排列
        Comparator<Products> byNum = new Comparator<Products>() {
            @Override
            public int compare(Products o1, Products o2) {
                if (o1.getNum() != o2.getNum()) {
                    return o1.getNum() < o2.getNum() ? -1 : 1;
                } else {
                    return o1.getPrice() < o2.getPrice() ? 1 : -1;
                }

            }
        };
        Collections.sort(products, byNum);

    }
}
